using System;
using System.Linq;
using System.Text;
using Publish.Enum;
using System.Collections.Generic;

namespace Publish
{
    /*****************************************************************
     * 定义广播的数据格式
     * Type=OnLine,FromAdress=xxx,ToAdress=zzz,Message=mmm
     * 类型为上线广播  从xxx主机到zzz主机  信息是mmm       
     * CHAT这个就是我的信息我的信息 可能有各种=,的字符串
     * 这种就直接将CHAT去掉后 后面的都为mmm
    *****************************************************************/

    /// <summary>
    /// 定义数据报里面的几个字段
    /// </summary>
    public class Datagram
    {
        #region Property

        /// <summary>
        /// 数据报的类型
        /// </summary>
        public DatagramType Type { get; set; }

        /// <summary>
        /// 发送者的网络地址
        /// </summary>
        public String FromAddress { get; set; }

        /// <summary>
        /// 接收者网络地址
        /// </summary>
        public String ToAddress { get; set; }

        /// <summary>
        /// 项目名称
        /// </summary>
        public String ProjectName { get; set; }

        /// <summary>
        /// 数据报的信息
        /// </summary>
        public String Message { get; set; }

        /// <summary>
        /// 信息 Message的长度
        /// </summary>
        public int Length { get { return Message.Length; } }

        #endregion

        #region Method

        /// <summary>
        /// 重写下ToString
        /// </summary>
        /// <returns></returns>
        public override String ToString()
        {
            var sb = new StringBuilder();
            sb.AppendFormat("Type={0},", Type);
            sb.AppendFormat("FromAddress={0},", FromAddress);
            sb.AppendFormat("ToAddress={0},", ToAddress);
            sb.AppendFormat("ProjectName={0},", ProjectName);
            sb.AppendFormat("Message={0}", Message);
            return sb.ToString();
        }

        /// <summary>
        /// 将有效字符串转化成数据报
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Datagram Convert(String str)
        {
            var data = new Datagram();
            //前面不是CHAT主要是建立连接 取消连接等信号传送
            if (!str.StartsWith("CHAT"))
            {
                IDictionary<String, String> idict = new Dictionary<String, String>();

                var strlist = str.Split(',');//数据报字符串的各个键值对放进字典类
                foreach (var info in strlist.Select(t => t.Split('=')))
                {
                    idict.Add(info[0], info[1]);
                }

                data.Type = (DatagramType)System.Enum.Parse(typeof(DatagramType), idict["Type"]);
                data.FromAddress = idict["FromAddress"];
                data.ToAddress = idict["ToAddress"];
                data.ProjectName = idict["ProjectName"];
                data.Message = idict["Message"];
            }
            else
            {
                data.Type = (DatagramType)System.Enum.Parse(typeof(DatagramType), "Chat");
                data.Message = str.Substring(4);//排除前面的 Chat 字符串
            }

            return data;
        }

        #endregion
    }
}
